//
//  SearchResultViewController.m
//  Funny
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 King. All rights reserved.
//

#import "SearchResultViewController.h"

@interface SearchResultViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UISearchBar *search;

@end

@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handleData];
    self.navigationItem.titleView = self.search;
    // Do any additional setup after loading the view.
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (UISearchBar *)search
{
    if (!_search) {
        self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 30, ScreenWidth, 30)];
        _search.tintColor = [UIColor whiteColor];
        _search.placeholder = @"搜索目的地/景点/酒店";
        _search.barTintColor = [UIColor whiteColor];
        _search.searchBarStyle = UISearchBarStyleMinimal;
        _search.delegate = self;
    }
    return _search;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.search.showsCancelButton = YES;
    for (UIView *view in [[_search.subviews lastObject] subviews]) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *cancelBtn = (UIButton *)view;
            [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.search resignFirstResponder];
    self.search.showsCancelButton = NO;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.search resignFirstResponder];
    self.search.showsCancelButton = NO;
    self.cityName = searchBar.text;
    self.dataArr = [NSMutableArray array];
    [self handleData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.search resignFirstResponder];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = NO;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"tableView"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)handleData
{
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/suggestion?q=%@",self.cityName];
    [NetHandler getDataWithURL:url completion:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dic objectForKey:@"dataother"];
        for (NSDictionary *dataDic in arr) {
            SearchModel *model = [SearchModel modelWithDic:dataDic];
            [self.dataArr addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self.search resignFirstResponder];
}

#pragma mark -tableView方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView"];
    cell.model = _dataArr[indexPath.row];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:[_dataArr[indexPath.row] name]];
    NSRange rang = [[_dataArr[indexPath.row] name] rangeOfString:self.cityName];
    [str setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0 green:220.0 / 256.0 blue:220.0 / 256.0 alpha:1.0],NSFontAttributeName:[UIFont systemFontOfSize:18]} range:rang];
    cell.name.attributedText = str;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainAnuseViewController *mainVC = [MainAnuseViewController shareViewController];
    mainVC.ID = [_dataArr[indexPath.row] ID];
    [self.navigationController pushViewController:mainVC animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
